package com.gombe.credit.app.ui.fragment

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import com.gombe.credit.app.R
import com.gombe.credit.app.common.CommonFragment
import com.gombe.credit.app.configs.GombeCreditConfigs
import com.gombe.credit.app.databinding.FragmentGombeCreditUserCenterBinding
import com.gombe.credit.app.event.GombeCreditEventData
import com.gombe.credit.app.event.GombeCreditEventPointType
import com.gombe.credit.app.ui.activity.ActGombeCreditPrivacyPolicy
import com.gombe.credit.app.ui.activity.ActGombeCreditUserAdviceType
import com.gombe.credit.app.ui.activity.ActGombeCreditUserSetting
import com.gombe.credit.app.utils.GombeCreditDensityUtil
import com.gombe.credit.app.utils.GombeCreditTitleBarUtil
import com.gombe.credit.app.utils.helper.GombeCreditConfigsHelper
import com.gombe.credit.app.utils.helper.GombeCreditCustomDataHelper
import com.gombe.credit.app.utils.shared.GombeCreditSharedHelper
import com.gombe.credit.app.utils.shared.GombeCreditSharedKey
import com.gombe.credit.lib.adapter.GombeCreditOnSingleClickListener

/**
 * GombeCredit
 * @Author wwdeng
 * @CreateTime: 2024/12/10 14:53
 * @Annotation：我的
 */
class FragmentGombeCreditUserCenter(): CommonFragment() {
    private var _binding: FragmentGombeCreditUserCenterBinding? = null
    private val binding get() = _binding ?: throw IllegalStateException(
        "gombe credit user center binding because it is null. Either the view hasn't been created yet, or it has already been destroyed."
    )

    override fun takeGombeCreditRootView(inflater: LayoutInflater): View {
        _binding = FragmentGombeCreditUserCenterBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initGombeCreditView() {
        setGombeCreditRootParentBackgroundColor(getColor(R.color.white))
        _binding?.userCenterLayout?.setPadding(0, GombeCreditTitleBarUtil.getStatusBarHeight() + GombeCreditDensityUtil.dp2px(10f), 0, 0)
        initUserCenterRefreshView()
    }

    override fun initGombeCreditListener() {
        _binding?.imgCustomer?.setOnClickListener(object : GombeCreditOnSingleClickListener(){
            override fun onSingleClick(v: View?) {
                _binding?.contactUsLayout?.callOnClick()
            }
        })
        _binding?.contactUsLayout?.setOnClickListener(object : GombeCreditOnSingleClickListener(){
            override fun onSingleClick(v: View?) {
                showCustomerDialog("User Center Page")
                addEventPointRequestAction(GombeCreditEventPointType.GOMBECREDIT_TRACK_CLICK.CUSTOMER,"User Center Page")
            }
        })
        _binding?.privacyPolicyLayout?.setOnClickListener(object : GombeCreditOnSingleClickListener(){
            override fun onSingleClick(v: View?) {
                val showTitle = getString(R.string.gombe_credit_privacy_policy_title)
                val linkUrl = GombeCreditConfigs.GOMBECREDIT_PRIVACY_POLICY_SENEGAL_LINK
                ActGombeCreditPrivacyPolicy.start(requireContext(), showTitle, linkUrl)
            }
        })
        _binding?.adviceLayout?.setOnClickListener(object : GombeCreditOnSingleClickListener(){
            override fun onSingleClick(v: View?) {
                onIntent(ActGombeCreditUserAdviceType::class.java)
            }
        })
        _binding?.settingLayout?.setOnClickListener(object : GombeCreditOnSingleClickListener(){
            override fun onSingleClick(v: View?) {
                onIntent(ActGombeCreditUserSetting::class.java)
            }
        })
    }

    @SuppressLint("SetTextI18n")
    override fun takeGombeCreditData() {
        addEventPointRequestAction(GombeCreditEventPointType.GOMBECREDIT_TRACK_PAGE.USER_CENTER_START)
        _binding?.textUserMobile?.text = "+${GombeCreditSharedHelper.takeGombeCreditSharedString(GombeCreditSharedKey.GOMBECREDIT_TIMEZONE_CODE)} ${GombeCreditSharedHelper.takeGombeCreditSharedString(GombeCreditSharedKey.GOMBECREDIT_MOBILE)}"

        //是否有新版本
        val isUpdate = GombeCreditConfigsHelper.isGombeCreditUpdate()
        if (isUpdate){
            _binding?.updateViewPoint?.visibility = View.VISIBLE
        } else {
            _binding?.updateViewPoint?.visibility = View.GONE
        }

        //加载保存自定义数据
        GombeCreditCustomDataHelper.takeGombeCreditCustomDataAction()
    }

    /* 初始化刷新 */
    private fun initUserCenterRefreshView() {
        _binding?.userRefreshLayout?.setColorSchemeResources(R.color.theme,R.color.purple_500)
        _binding?.userRefreshLayout?.setOnRefreshListener {
            GombeCreditCustomDataHelper.takeGombeCreditCustomDataAction()
        }
    }

    override fun onGombeCreditPublishEvent(event: GombeCreditEventData) {
        super.onGombeCreditPublishEvent(event)
        when(event.eventType){
            GombeCreditEventData.TYPE.GOMBECREDIT_CUSTOM_DATA_REFRESHED,
            GombeCreditEventData.TYPE.GOMBECREDIT_NET_ERROR -> {
                hideGombeCreditLoading()
            }
            else -> {

            }
        }
    }

    override fun hideGombeCreditLoading() {
        super.hideGombeCreditLoading()
        _binding?.userRefreshLayout?.isRefreshing = false
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    override fun onDestroy() {
        super.onDestroy()
        addEventPointRequestAction(GombeCreditEventPointType.GOMBECREDIT_TRACK_PAGE.USER_CENTER_END)
    }
}